package com.app.twth.ui.checkin;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.databinding.ItemCheckinContainerListBinding;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;


public class CheckinContainerAdapter extends RecyclerView.Adapter<CheckinContainerAdapter.MyViewHolder> {
    private LayoutInflater mInflater;
    private MyOnClickListener locationListener;
    private List<Container> data = new ArrayList<>();

    public CheckinContainerAdapter(Context mContext, List<Container> data, MyOnClickListener locationListener) {
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.locationListener = locationListener;
    }

    public void setData(List<Container> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemCheckinContainerListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemCheckinContainerListBinding itemBinding;

        public MyViewHolder(ItemCheckinContainerListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            Container receiving = data.get(position);
            itemBinding.tvContainerNo.setText(receiving.getContainerNo() + "");
            itemBinding.tvInboundNo.setText(Utils.getStr(receiving.getInboundNo()) + "");
            if (Utils.getStr(receiving.getInboundNo()).equals("")) {
                itemBinding.tvInboundNo.setVisibility(View.GONE);
            } else {
                itemBinding.tvInboundNo.setVisibility(View.VISIBLE);
            }
            itemBinding.tvExpected.setText(Utils.getStr(receiving.getForecastArrivedDate()) + "");
            itemBinding.tvDropped.setText(Utils.getStr(receiving.getArrivedDate()) + "");
            itemBinding.layout.setOnClickListener(v -> {
                locationListener.OnClick(receiving);
            });
        }
    }

    public interface MyOnClickListener {
        void OnClick(Container receiving);
    }
}
